package com.tal.bcc.pad.cloud.coreprogress;

import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.BufferedSink;
import okio.Okio;

class ProgressRequestBody extends RequestBody {
    private final RequestBody mRequestBody;
    private final ProgressCallback progressListener;

    ProgressRequestBody(RequestBody requestBody, ProgressCallback progressListener) {
        this.mRequestBody = requestBody;
        this.progressListener = progressListener;
    }

    public MediaType contentType() {
        return this.mRequestBody.contentType();
    }

    public long contentLength() throws IOException {
        return this.mRequestBody.contentLength();
    }

    public void writeTo(BufferedSink sink) throws IOException {
        if (this.progressListener == null) {
            this.mRequestBody.writeTo(sink);
        } else {
            ProgressOutputStream progressOutputStream = new ProgressOutputStream(sink.outputStream(), this.progressListener, this.contentLength());
            BufferedSink progressSink = Okio.buffer(Okio.sink(progressOutputStream));
            this.mRequestBody.writeTo(progressSink);
            progressSink.flush();
        }
    }
}
